<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><? echo $title ?></title>
<? require("lib.php"); ?>
    <style>
        .tag_choosed{ margin-left: 5px; margin-right: 5px}
        .tag_choose{margin-left: 5px; margin-right: 5px}
    </style>
</head>
<body>
  <div class="content-box"><!-- Start Content Box -->
    <div class="content-box-header">
      <h3><? echo $title ?></h3>
      <div class="clear"></div>
    </div> <!-- End .content-box-header -->
    <div class="content-box-content">
<?
  switch ($do){
    case "add":
    add();
    break;
    case "update":
    update($info);
    break;
    case "delete":
    delete($id);
    break;
    case "flip": flip($id); break;
    case "featured": flip($id,'is_featured'); break;
    default:
    show_info($info);
  }
?>
<? function show_box($row, $iseditmode, $isadd=0){
		global $id, $h, $url,$title,$cate,$baseurl,$option,$option_module,$arrlang,$lang,$code_array,$arrcurrency,$db_default_currency_id,$filter, $cross_sell,$page, $tag, $tag_list_info, $str_tag;
    $is_enabled=1; if(isset($row['is_enabled']))$is_enabled=$row['is_enabled'];
    $is_virtual=0; if(isset($row['is_virtual']))$is_virtual=$row['is_virtual'];
    $unit=$code_array['product_unit'];
    // build category tree 构建分类级别树
    $tem = $cate->get_rows(1024,'','','seq_no');
    $tree=unlimitedlevel($tem);
    $cate_array=array();
    foreach($tree as $val){
      $cate_array[$val['id']]=array(
        'id'=>$val['id'],
        'name'=>$val['html_prefix'].$val['name_'.$lang],
        );
    }
    //cross sell
    $str_cross_sell='';
    if(!$isadd){
      $tem=$cross_sell->get_rows(128,'product_id',$id,'cross_product_id','asc');
      foreach($tem as $val) $str_cross_sell.=','.$val['cross_product_id'];
      $str_cross_sell=substr($str_cross_sell,1);
    }
	?>
	<table width="99%" cellspacing="0" cellpadding="3" class="mbox">
      <tr class="th"><td class="thtitle" width="120" align="right"><? echo $title ?></td>
        <td><? echo $h->hidden('filter', $filter); ?><? echo $h->hidden('id', iso($row["id"])); ?><? echo $h->hidden('page', $page); ?></td></tr>
      <?
        $str='';
        foreach($arrlang as $val){ // 加载所有启用的分语言的字段, add all fields that marked by languages
          $str.='<tr><td class="l">'.ROOT_NAME.'('.$val['name'].')</td><td>'.$h->text('name_'.$val['code'], iso($row["name_".$val['code']]), 'g').'</td></tr>';
          $str.='<tr><td class="l">'.ROOT_DESCRIPTION.'('.$val['name'].')</td><td>'.$h->textarea('description_'.$val['code'], iso($row["description_".$val['code']]), 32, 16, 'editor g', '').'</td></tr>';
          $str.='<tr><td class="l">'.ROOT_FEATURE.'('.$val['name'].')</td><td>'.$h->textarea('feature_'.$val['code'], iso($row["feature_".$val['code']]), 32, 16, 'editor g', '').'</td></tr>';
          $str.='<tr><td class="l">'.ROOT_SPECIFICATION.'('.$val['name'].')</td><td>'.$h->textarea('spec_'.$val['code'], iso($row["spec_".$val['code']]), 32, 16, 'editor g', '').'</td></tr>';
          //$str.='<tr><td class="l">'.ROOT_OTHER1.'('.$val['name'].')</td><td>'.$h->textarea('other1_'.$val['code'], iso($row["other1_".$val['code']]), 32, 16, 'editor g', '').'</td></tr>';
        }
        echo $str;
      ?>
      <tr><td class="l"><?=ROOT_CATEGORY?></td><td><? echo $h->select('category_id', $row["category_id"], $cate_array, 'id', 'name', 'g') ?></td></tr>
      <tr><td class="l"><?=ROOT_PICTURE?></td><td>
      <? if(!$isadd)echo '<img src="'.$baseurl.'upload/'.decode($row["small_pic"]).'" height="72" /><br />'; ?>
      <? echo $h->cfile('cpic', 'g') ?> <span class="red"> <?=ROOT_PICTURE_SIZE_RATIO?><? echo PRODUCT_LARGE_PIC_WIDTH.':'.PRODUCT_LARGE_PIC_HEIGHT ?>.</span>
      </td></tr>
      <tr><td class="l"><?=ROOT_SKU?></td><td><? echo $h->text('sku', iso($row["sku"]), 'gs') ?> &nbsp; &nbsp; <?=ROOT_UNIT?> &nbsp; <? echo $h->select('unit', $row["unit"], $unit, 'id', 'name_'.$lang, 'gs') ?></td></tr>
      <tr><td class="l"><?=ROOT_PRICE?></td><td><? echo $h->text('price', iso($row["price"]), 'gs') ?> <?=$arrcurrency[$db_default_currency_id]['code']?> &nbsp; &nbsp; <?=ROOT_WEIGHT?> &nbsp; <? echo $h->text('weight', iso($row["weight"]), 'gmin') ?> g</td></tr>
      <tr><td class="l"><?=ROOT_STOCK?></td><td><? echo $h->text('stock', iso($row["stock"]), 'gs') ?> &nbsp; &nbsp; <?=ROOT_SAFETY_STOCK?> &nbsp; <? echo $h->text('safety_stock', iso($row["safety_stock"]), 'gmin') ?> <span class="red"> <?=ROOT_STOCK_IS_INTEGER?></span></td></tr>
      <? $nada='hide'; if(is_module_enabled($option_module['cross_sell'])) $nada='';?>
      <tr class="<?=$nada?>"><td class="l"><?=ROOT_CROSS_SELL?></td><td><? echo $h->textarea('cross_sell', $str_cross_sell, 24, 2, 'g') ?><br /><span class="red"> <?=ROOT_CROSS_SELL_TIP?></span></td></tr>
      
      <tr><td class="l"><?=ROOT_ORDER?></td><td><? echo $h->text('seq_no', iso($row["seq_no"]), 'gmin') ?> <span class="red"> <?=ROOT_HIGHER_NUMBER_IN_FRONT?></span></td></tr>
      <tr><td class="l"><?=ROOT_OTHER?></td><td>&nbsp; <? echo $h->checkbox('is_enabled',$is_enabled); ?> <?=ROOT_ON_SHELF?> &nbsp; <? echo $h->checkbox('is_virtual',$is_virtual); ?> <?=ROOT_VIRTUAL_ITEM?> </td></tr>
      <tr><td class="l"><?=ROOT_TAGS_CHOOSE?></td><td class="add_tag_child">&nbsp;
              <input type="hidden" name="tag_list_str" value="<?=$str_tag?>"/>
          <?
          $str_tag = '';
          $tag_id_arr = array();
          if($tag){
              foreach($tag as $key =>$val){
                  $str_tag .= '<a class="tag_choosed" href="javascript:void(0)" onclick="remove_tag(this)"><input type="hidden" name="tag_id" value="'.$val['tag_id'].'"/>'.$val['tag_name'].'</a>';
                  $tag_id_arr[] = $val['tag_id'];
              }
          }
          $str_tag .= '&nbsp;<input type="text" class="gs tags_add_value" value="" name="tags_add">&nbsp;<a class="tags_add" onclick="tags_add_value()">'.ROOT_TAGS_ADD.'</a> &nbsp;<a class="tag_show" href="javascript:void(0)">'.ROOT_TAGS_COMMON_USE.'</a>';
          echo $str_tag;
          ?>
          </td></tr>
        <tr class="tag_list_show hide"><td></td><td>
                <?foreach($tag_list_info as $key => $val){
                    $onclick = in_array($val['id'], $tag_id_arr) ? '' : 'onclick="add_tag(this)"';
                    echo '<a class="tag_choose" href="javascript:void(0)" '.$onclick.'><input type="hidden" name="tag_id" value="'.$val['id'].'">'.$val['name_'.$lang].'</a>';
                }?>
        </td></tr>
      <tr><td class="l">&nbsp;</td><td><br><? echo $h->submit('save', '&nbsp; '.ROOT_SAVE.' &nbsp;', $iseditmode==1?'button':'hide') ?>&nbsp;&nbsp;<a onClick="history.go(-1)">[ <?=ROOT_CANCEL?> ]</a><br><br></td></tr>
    </table>
    <? } ?>
			<? function show_boxes($result){
				global $url, $page, $pages, $cateid, $cate,$h, $s, $t, $lang, $filter, $option_module, $total_products, $baseurl, $gee_tags, $gee_tag_list, $tag, $tag_list_info, $str_tag;

				$str='<table width="99%" class="mlist"><tr>';  // 搜索
				$str.='<td><form action="'.$url.'.php" method="get"><input type="hidden" name="filter" value="'.$filter.'"/><input placeholder="'.ROOT_SEARCH_BY_PRODUCT_NAME_AND_SKU.'" name="s" class="g" value="'.$s.'" /> &nbsp;<input placeholder="'.ROOT_TAGS_NAME.'" name="t" class="g" value="'.$t.'" /> &nbsp; <input type="submit" class="button_blue" value=" '.ROOT_SEARCH.' " /></form></td></tr></table>';
                echo $str;
                echo $h->form($url.'.php', 0, 'myform'); // 构建删除多项表单 form for deleting muti rows
                echo $h->hidden('action', ''); //
                echo $h->hidden('page', $page); // 当前页面 current page
                echo $h->hidden('s', $s); // 当前页面 current page
                echo $h->hidden('filter', $filter); // 当前页面 current page
        		$str='<table width="99%" cellspacing="0" class="mlist"><tr class="th"><td width="48"><input type="checkbox" name="aller" class="check-all" /> '.ROOT_SELECT_ALL.'</td><td><strong>#</strong></td><td><strong>'.ROOT_NAME.'</strong></td><td><strong>'.ROOT_PICTURE.'</strong></td><td><strong>'.ROOT_SKU.'</strong></td><td><strong>'.ROOT_TAGS_CHOOSE.'</strong></td><td><strong>'.ROOT_CATEGORY.'</strong></td><td><strong>'.ROOT_PRICE.'</strong></td><td><strong>'.ROOT_STOCK.'</strong></td><td><strong>'.ROOT_SOLD_RECORD.'</strong></td>';
				$str.='<td>';
				if($total_products<MAX_PRODUCT_ALLOW) {
					$str.='<a '.(($filter!='')?"class='nada'":"").' href="'.$url.'.php?do=add">[ '.ROOT_ADD.' ]</a>';
				}
				else {
					$str.='[ '.ROOT_PRODUCT_UPLOAD_QUANTITY_REACH_MAX_LIMIT.' ]';
				}
				$str.='</td>';
				$str.='</tr>';
				$count=1;
        $tem = $cate->get_rows(1024);
        $cate_array=array();
        foreach($tem as $val){
          $cate_array[$val['id']]=$val['name_'.$lang];
        }
        foreach($result as $row)
				{
					$id=decode($row['id']);
					$enable=$row['is_enabled']?ROOT_OFF_SHELF:ROOT_ON_SHELF;
					$featured=$row['is_featured']?ROOT_OFF_RECOMMAND:ROOT_ON_RECOMMAND;
					$cate_name=$cate_array[$row['category_id']];
          $tem_pic='-';
                    $where = array();
                    $where[]=array('name'=>'p_id','val'=>$id);
                    $where[]=array('name'=>'tags_table','val'=>1);
                    $tag = $gee_tag_list->get_rows_advanced(1024, $where);

//    $tag = $gee_tag_list->get_rows(1024, 'p_id', $id);
            $arr_tag = array();
            for($i = 0; $i < count($tag); $i++){
                $tag_info = $gee_tags->get_row($tag[$i]['tag_id']);
                $tag[$i]['tag_name'] = $tag_info['name_'.$lang];
                $arr_tag[] = $tag[$i]['tag_name'];
            }
            $tag_name = empty($arr_tag) ? '' : implode('<br>', $arr_tag);
          if(!empty($row["small_pic"]))$tem_pic='<img src="'.$baseurl.'upload/'.decode($row["small_pic"]).'" height="60" />';
          
					$str.="<tr ".(($row['is_enabled'])?"":"class=\"disabled\"")."><td><input class='aller' type='checkbox' name='all[]' value='".$id."' /></td><td>".$id.".</td><td><a href=\"$url.php?cateid=$cateid&page=$page&do=update&id=".decode($row['id']).(($filter=="")?"":"&filter=out_of_stock")."\">".decode($row['name_'.$lang])."</a></td><td>".$tem_pic."</td><td>".$row['sku']."</td><td>".$tag_name."</td><td>".$cate_name."</td><td>".$row['price']."</td><td>".$row['stock']."</td><td>".$row['sold_quantity']."</td><td>";
					if($filter=='')$str.="<a href=\"product_pic.php?cid=".decode($row['id'])."\">".ROOT_PICTURE_GALLERY."</a> | ";
          if($filter==''&&is_module_enabled($option_module['product_review']))$str.="<a href=\"product_review.php?cid=".decode($row['id'])."\">".ROOT_REVIEW."</a> | ";
          if($filter==''&&is_module_enabled($option_module['product_variation']))$str.="<a href=\"product_var.php?cid=".decode($row['id'])."\">".ROOT_VARIATION."</a> | ";
					$str.="<a href=\"$url.php?cateid=$cateid&page=$page&do=update&id=".decode($row['id']).(($filter=="")?"":"&filter=out_of_stock")."\">".ROOT_UPDATE."</a> | ";
					$str.="<a href=\"$url.php?cateid=$cateid&page=$page&do=delete&id=".decode($row['id'])."\" onClick=\"javascript: return delc();\">".ROOT_DELETE."</a> | ";
					$str.="<a href=\"$url.php?do=flip&cateid=$cateid&page=$page&id=".$id.(($filter=="")?"":"&filter=out_of_stock")."\">".$enable."</a> | ";
					if($filter=='')$str.="<a href=\"$url.php?do=featured&cateid=$cateid&page=$page&id=".$id."\">".$featured."</a>";
					$str.="</td></tr>";
					$count++;
				}
//                $str.="<tr><td colspan='8'> <input class='button del_all' type='submit' value='".ROOT_DELETE_SELECTED."' onClick=\"javascript: return delc();\" /></td></tr></table>";
				$str.= "</table>";
				echo $str;
				if($count==1) echo "<br>".ROOT_NO_DATA."!";
                $h->former();
                ?>

                <table width="99%" cellspacing="0" class="mlist">
                <tr><td colspan="2"><input class="button" onclick="check_all('deletes');return operator();" type="submit" value="<?=ROOT_DELETE_SELECTED?>" />&nbsp;<input class="button" onclick="check_all('is_enable');return operator();" type="submit" value="<?=ROOT_PRODUCT_IS_ENABLE?>" />&nbsp;<input class="button" onclick="check_all('dis_enable');return operator();" type="submit" value="<?=ROOT_PRODUCT_DIS_ENABLE?>" />&nbsp;<a class="button tag_show" href="javascript:void(0)"><?=ROOT_TAGS_ADD?></a></td></tr>
                <tr class="hide tags_list_show"><td width="100"><?=ROOT_TAGS_CHOOSE?></td><td class="add_tag_child">&nbsp;<input type="hidden" name="tag_list_str" value=""/>
                        &nbsp;<input type="text" class="gs tags_add_value" value="" name="tags_add">&nbsp;<a class="tags_add" onclick="tags_add_value()"><?=ROOT_TAGS_ADD?></a>&nbsp;<input class="button" onclick="check_all('tags_add');return operator();" type="submit" value="<?=ROOT_ALL_ADD?>"/>
                    </td></tr>
                <tr class="hide tag_list_show tags_list_show"><td width="100"><?=ROOT_TAGS_COMMON_USE?></td><td class="tag_list_show_i">
                        <?foreach($tag_list_info as $key => $val){
                            $onclick = 'onclick="add_tag(this)"';
                            echo '<a class="tag_choose" href="javascript:void(0)" '.$onclick.'><input type="hidden" name="tag_id" value="'.$val['id'].'">'.$val['name_'.$lang].'</a>';
                        }?>
                </td></tr>
                </table>

             <?
                echo echo_page($page, $pages, $url.'-'.$cateid.'-p');
            } ?>
<script type="text/javascript">
   /* $(function(){
        $('.del_all').click(function(){
            $('.myform input:hidden[name="action"]').val('deletes');
            $('.myform').submit();
        });
    });*/
    $(function(){
        var k = 1;
        $('.tag_show').click(function(){
            if(k){
                $('.tag_list_show').removeClass('hide');
                $('.tags_list_show').removeClass('hide');
                k = 0;
            }else{
                $('.tag_list_show').addClass('hide');
                $('.tags_list_show').addClass('hide');
                k = 1;
            }
        });
    });

    function tags_add_value(){
        var tags_value = $('.tags_add_value').val();
            $.ajax({
                'url':'tag_product_add.php?do=tag_add',
                'data':{'value':tags_value},
                'type':'post',
                success:function(data){
                    var data_arr = eval("("+data+")");
                    if(data_arr.id){
                    $('.tags_add_value').before('<a class="tag_choosed" href="javascript:void(0)" onclick="remove_tag(this)"><input type="hidden" name="tag_id" value="'+data_arr.id+'"/>'+data_arr.value+'</a>');
                    $('.tag_choose').first().before('<a class="tag_choose" href="javascript:void(0)"><input type="hidden" name="tag_id" value="'+data_arr.id+'"/>'+data_arr.value+'</a>');
                    var tag_list_str = $('input:hidden[name="tag_list_str"]').val();
                    tag_list_str += ','+data_arr.id;
                    if(tag_list_str[0] == ','){
                        tag_list_str = tag_list_str.substring(1, tag_list_str.length);
                    }
                    $('input:hidden[name="tag_list_str"]').val(tag_list_str);
                }
            }
        });
    }

    function add_tag(node){
        $('.tags_add_value').before('<a class="tag_choosed" href="javascript:void(0)" onclick="remove_tag(this)">'+$(node).html()+'</a>');
        var tag_id = $(node).children('input:hidden').val();
        var tag_list_str = $('input:hidden[name="tag_list_str"]').val();
        tag_list_str += ','+tag_id;
        if(tag_list_str[0] == ','){
            tag_list_str = tag_list_str.substring(1, tag_list_str.length);
        }
        $('input:hidden[name="tag_list_str"]').val(tag_list_str);
        $(node).removeAttr('onclick');
    }
    function remove_tag(node){
        $(node).remove();
        var tag_id = $(node).children('input:hidden').val();
        var tag_list_str = $('input:hidden[name="tag_list_str"]').val();
        var tag_arr = tag_list_str.split(',');
        tag_arr.splice($.inArray(tag_id, tag_arr), 1);
        tag_list_str= tag_arr.join(',');
        $('input:hidden[name="tag_list_str"]').val(tag_list_str);
        $('.tag_list_show a').each(function(){
            if(!($(this).attr('onclick')) &&　($(this).children('input:hidden').val() == tag_id)){
                $(this).attr('onclick', 'add_tag(this)');
            }
        });
    }
    function check_all(otype){
        $('.myform input:hidden[name="action"]').val(otype);
        $('.myform').submit();
    }
</script>
         <br class="clear" />
				</div> <!-- End .content-box-content -->
                     <br class="clear" />
			</div><!-- End .content-box -->
</body>
</html>
